﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class Extensions
    {
        public static string Substring(this string source, string start, string end = null, bool includeStart = false, bool includeEnd = false)
        {
            return Substring(source, start, 0, end, includeStart, includeEnd);
        }

        public static string Substring(this string source, string start, int startIndex, string end = null, bool includeStart = false, bool includeEnd = false)
        {
            int indexOfStart = source.IndexOf(start, startIndex);

            if (!includeStart)
                indexOfStart += start.Length;

            return Substring(source, indexOfStart, end, includeStart, includeEnd);
        }

        public static string Substring(this string source, int indexOfStart, string end = null, bool includeStart = false, bool includeEnd = false)
        {
            int indexOfEnd;
            if (string.IsNullOrEmpty(end))
                indexOfEnd = source.Length;
            else
                indexOfEnd = source.IndexOf(end, indexOfStart);


            if (includeEnd && !string.IsNullOrEmpty(end))
                indexOfEnd += end.Length;

            return source.Substring(indexOfStart, indexOfEnd - indexOfStart);
        }
    }
}
